"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/11/6 8:42
@Description : 
装饰使用场景
"""
# 定义单例装饰器
def singleton(cls):
    instances = {}

    def inner(*args, **kw):
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]

    return inner


@singleton
class Dog:
    pass

@singleton
class Cat:
    pass


# 通过调用两次无参构造 创建出的对象不是同一个对象
# 如何实现全局dog对象都是同一个  单例模式
dog1 = Dog()
print(f'dog1 id:{id(dog1)},type:{type(dog1)}')
dog2 = Dog()
print(f'dog2 id:{id(dog2)},type:{type(dog2)}')
dog3 = Dog()
print(f'dog3 id:{id(dog3)},type:{type(dog3)}')


cat1 = Cat()
print(f'cat1 id:{id(cat1)},type:{type(cat1)}')
cat2 = Cat()
print(f'cat2 id:{id(cat2)},type:{type(cat2)}')